package cn.legend.travel.ask.dao.persist.repository.impl;

import cn.legend.travel.ask.dao.persist.mapper.AsksMapper;
import cn.legend.travel.ask.dao.persist.mapper.UserCollectionMapper;
import cn.legend.travel.ask.dao.persist.repository.AsksRepository;
import cn.legend.travel.ask.pojo.entity.Ask;
import cn.legend.travel.ask.pojo.entity.UserCollection;
import cn.legend.travel.ask.pojo.param.AddNewAsksParam;
import cn.legend.travel.ask.pojo.vo.*;
import cn.legend.travel.common.pojo.vo.PageData;
import cn.legend.travel.common.util.PageInfoToPageDataConverter;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
@Slf4j
public class AsksRepositoryImpl implements AsksRepository {
    @Autowired
    private AsksMapper asksMapper;
    @Autowired
    UserCollectionMapper userCollectionMapper;
    @Override
    public List<AskVo> getAsks(Integer pageNum, Integer pageSize,String sort) {
        pageNum = pageNum * pageSize;
        return asksMapper.getAsks(pageNum,pageSize,sort);
    }

    @Override
    public int addAsks(AddNewAsksParam asksParam) {
        Ask ask = new Ask();
        BeanUtils.copyProperties(asksParam,ask);
        return asksMapper.insert(ask);
    }

    @Override
    public List<UserSortVo> getBestUsers(Date date,String sort) {
        log.error("data{},sort{}",date,sort);
        return asksMapper.getBestUsers(date,sort);
    }

    @Override
    public PageData<AnswerVo> getAnswers(Long questionId,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return PageInfoToPageDataConverter.convert(new PageInfo<>(asksMapper.getAnswers(questionId)));
    }

    @Override
    public Ask getAsk(Long questionId) {
        return asksMapper.selectById(questionId);
    }

    @Override
    public int addLikes(Long questionId) {
        return asksMapper.addLikes(questionId);
    }

    @Override
    public int cancelLikes(Long questionId) {
        return asksMapper.cancelLikes(questionId);
    }

    @Override
    public int addView(Long questionId) {
        return asksMapper.addView(questionId);
    }

    @Override
    public int report(Long questionId) {
        return asksMapper.report(questionId);
    }

    @Override
    public List<AsksCitySimpleVO> getByCity(String city) {
        return asksMapper.getByCity(city);
    }

    @Override
    public UserCollection getCollection(Long questionId, Long id, String asks) {
        UserCollection userCollection = userCollectionMapper.selectByIds(questionId, id, asks);
        log.error("dawdawd{}",userCollection);
        return userCollection;
    }

    @Override
    public int insert(UserCollection userCollection) {
        log.error("dawdadP{}",userCollection);
        return userCollectionMapper.insert(userCollection);
    }

    @Override
    public Ask getAskById(Long questionId) {

        Ask ask = asksMapper.selectById(questionId);
        System.out.println(ask);
        return ask;
    }

}
